import React, { Component } from 'react'

import { Link, withRouter } from "react-router-dom"

import menuList from '../../config/menuConfig/index.js';
import { Menu, Icon } from 'antd';
import "./left-nav.less";
const { SubMenu } = Menu;
class LeftNav extends Component {
    getMenuNodes = (menu) => {
        let { pathname } = this.props.location
        return menu.map(item => {
            if (item.children) {
                const cItem = item.children.find(c => c.path === pathname)
                if (cItem) {
                    this.openKey = item.path
                }
                return (<SubMenu
                    key={item.path}
                    title={
                        <span>
                            <Icon type={item.icon} />
                            <span>{item.title}</span>
                        </span>
                    }
                >
                    {this.getMenuNodes(item.children)}
                </SubMenu>)
            } else {
                return (<Menu.Item key={item.path}>
                    <Link to={item.path}>
                        <Icon type={item.icon} />
                        <span>{item.title}</span>
                    </Link>
                </Menu.Item>)
            }
        })
    }
    UNSAFE_componentWillMount() {
        this.menuNodes = this.getMenuNodes(menuList)
    }
    render() {
        let { pathname } = this.props.location
        return (
            <div className="left-nav-container">
                <Link to="/home">
                    <div className="left-nav-header">
                        vvmily系统
                    </div>
                </Link>
                <Menu
                    mode="inline"
                    theme="dark"
                    selectedKeys={[pathname]}
                    defaultOpenKeys={[this.openKey]}
                >
                    {
                        this.menuNodes
                    }
                </Menu>
            </div>
        )
    }
}


export default withRouter(LeftNav)